package com.sunzhe.cms.service;

import com.ibeetl.admin.core.dao.*;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.DataAccessFactory;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.beetl.DataAccessFunction;
import com.ibeetl.cms.dao.CmsArticleDao;
import com.ibeetl.cms.dao.CmsCatalogDao;
import com.ibeetl.cms.dao.CmsCatalogDictDao;
import com.ibeetl.cms.entity.CmsArticle;
import com.ibeetl.cms.entity.CmsCatalog;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.SQLPlaceholderST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * cms组件功能访问入口，所有方法应该支持缓存或者快速访问
 * @author xiandafu
 */
@Service
public class CmsPlatformService {

    /*当前用户会话*/
    public static final String ACCESS_CURRENT_USER = "cms:user";
    public static final String ACCESS_SUPPER_ADMIN = "admin";
    
	@Autowired
	HttpRequestLocal httpRequestLocal;

    @Autowired
    CoreRoleFunctionDao roleFunctionDao;

    @Autowired
    CoreRoleMenuDao sysRoleMenuDao;

    @Autowired
    CoreOrgDao sysOrgDao;

    @Autowired
    CoreRoleFunctionDao sysRoleFunctionDao;

    @Autowired
    CoreMenuDao sysMenuDao;
    
    @Autowired
    CmsCatalogDao cmsatalogDao;

    @Autowired
    CoreUserDao sysUserDao;

    @Autowired
    CoreFunctionDao sysFunctionDao;
    
    @Autowired
    CmsCatalogDao catalogDao;
    @Autowired
    CmsArticleDao articleDao;
    @Autowired
    CmsCatalogDictDao catalogDictDao;
    @Autowired
    SQLManager sqlManager;

    @Autowired
    DataAccessFunction dataAccessFunction;

    @Autowired
    CmsPlatformService self;
    @Autowired
    DataAccessFactory dataAccessFactory;

    @PostConstruct//此注释会在spring在启动的阶段就调用
    @SuppressWarnings("unchecked")
    public void init() {
        SQLPlaceholderST.textFunList.add("function");
        //sql语句里带有此函数来判断数据权限,在cire项目中的PlatformService中已经有定义function了，这里作为其他项目扩展用
        //sqlManager.getBeetl().getGroupTemplate().registerFunction("function", dataAccessFunction);
        //sqlManager.getBeetl().getGroupTemplate().registerFunction("nextDay", new NextDayFunction());
    }


    public CoreUser getCurrentUser() {
        CoreUser user =  (CoreUser) httpRequestLocal.getSessionValue(ACCESS_CURRENT_USER);
        return user;

    }
    
    public void setLoginUser(CoreUser user) {
    	httpRequestLocal.setSessionValue(CmsPlatformService.ACCESS_CURRENT_USER, user);
    }
    
    /**
     * 判断用户是否是超级管理员
     * @param user
     * @return
     */
    public boolean isSupperAdmin(CoreUser user) {
        return user.getCode().startsWith(ACCESS_SUPPER_ADMIN);
    }

    public boolean isCurrentSupperAdmin() {
        CoreUser user = this.getCurrentUser();
        return isSupperAdmin(user);
    }

    public List<Long> getDictIdByCatalog(Long catalogId) {
        List<Long> l = this.catalogDictDao.getDictIdByCatalog(catalogId);
        return l;
    }

    public List<CmsCatalog> getCatalogByDictTypeAndValue(String type, String value) {
        List<CmsCatalog> l = this.catalogDictDao.getCatalogByDictTypeAndValue(type, value, null);
        return l;
    }
    public List<CmsCatalog> getCatalogByDictTypeAndValue(String type, String value, Long parentCatalogId) {
        List<CmsCatalog> l = this.catalogDictDao.getCatalogByDictTypeAndValue(type, value, parentCatalogId);
        return l;
    }

    @Cacheable(value = CorePlatformService.CATALOG_DICT_TYPE_NAME)
    public List<CmsCatalog> getCatalogByDictTypeAndValueAndParentCatalogId(String type, String value, String parentCatalogId) {
        if (StringUtils.equals("null", parentCatalogId)) {
            System.out.println(1);
        }
        List<CmsCatalog> l = this.catalogDictDao.getCatalogByDictTypeAndValueAndParentCatalogId(type, value, parentCatalogId);
        return l;
    }
    @Cacheable(value = CorePlatformService.CATALOG_DICT_TYPE_NAME)
    public List<CmsCatalog> getCatalogByDictTypeAndValueAndParentCatalogId(String type, String value, Long parentCatalogId) {
        if (parentCatalogId != null) {
            System.out.println(1);
        }
        List<CmsCatalog> l = this.catalogDictDao.getCatalogByDictTypeAndValueAndParentCatalogId(type, value, String.valueOf(parentCatalogId));
        return l;
    }

    public boolean isAllowUserName(String name){
    	return !name.startsWith(ACCESS_SUPPER_ADMIN);
    }

    public CmsCatalog getCatalogById(String id) {
    	return this.catalogDictDao.getCatalogByCatalogId(NumberUtils.toLong(id));
    }
    public CmsArticle getArticleById(String id) {
    	return this.articleDao.single(NumberUtils.toLong(id));
    }

    @Cacheable(value = CorePlatformService.CATALOG_CACHE_CHILDREN)
    public List<CmsCatalog> findChildByParent(String id) {
        if (StringUtils.isBlank(id)) return null;
        return this.cmsatalogDao.findChildByParent(NumberUtils.toLong(id));
    }
    @Cacheable(value = CorePlatformService.CATALOG_CACHE_CHILDREN)
    public List<CmsCatalog> findChildByParent(Long id) {
        return this.cmsatalogDao.findChildByParent(id);
    }
}
